﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using Framework;

namespace Enterprise
{
    /// <summary>
    /// 系统设置管理
    /// </summary>
    public class SettingService : BaseService
    {
        /// <summary>
        /// 系统项设置
        /// </summary>
        /// <param name="colunm">列</param>
        /// <param name="value">值</param>
        /// <param name="language">语言</param>
        /// <returns></returns>
        public int Set(string colunm, string value, string language)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(*) from TbSetting  ");
            strSql.Append(" where CoLanguage=@CoLanguage;");
            SQLiteParameter[] parameters =
                {
                  new SQLiteParameter("@CoLanguage", DbType.String,255),
                  new SQLiteParameter("@"+colunm, DbType.String)
                };
            parameters[0].Value = language;
            parameters[1].Value = value;
            var count = DbHelperSqLite.GetSingle(strSql.ToString(), parameters).SafeToInt();
            var strSqlOne = new StringBuilder();
            if (count > 0)
            {
                strSqlOne.Append("update TbSetting set ");
                strSqlOne.AppendFormat("{0}=@{0}", colunm);
                strSqlOne.Append(" where CoLanguage=@CoLanguage;");
            }
            else
            {
                strSqlOne.AppendFormat("insert into TbSetting(CoLanguage,{0})", colunm);
                strSqlOne.AppendFormat(" values(@CoLanguage,@{0});", colunm);
            }
            var obj = DbHelperSqLite.ExecuteSql(strSqlOne.ToString(), parameters).SafeToInt();
            return obj;
        }

        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="colunm">获取ACTION 表的列</param>
        /// <param name="language"> </param>
        /// <returns></returns>
        public DataRow GetByLanguage(string colunm, string language)
        {
            SQLiteParameter[] parameters =
                {
                   new SQLiteParameter("@CoLanguage", DbType.String,255),
                   new SQLiteParameter("@"+colunm, DbType.String,255)
                };
            parameters[0].Value = language;
            parameters[1].Value = "";
            var strSqlOne = new StringBuilder();
            var rows = DbHelperSqLite.Query("select {0},CoLanguage from TbSetting where CoLanguage=@CoLanguage".FormatWith(colunm), parameters).Tables["ds"].Rows;
            if (rows.Count < 1)
            {
                strSqlOne.AppendFormat("insert into TbSetting(CoLanguage,{0})", colunm);
                strSqlOne.AppendFormat(" values(@CoLanguage,@{0});", colunm);
                DbHelperSqLite.ExecuteSql(strSqlOne.ToString(), parameters);
                rows = DbHelperSqLite.Query("select {0},CoLanguage from TbSetting where CoLanguage=@CoLanguage".FormatWith(colunm), parameters).Tables["ds"].Rows;
            }
            return rows[0];
        }

        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="language"> </param>
        /// <returns></returns>
        public DataRow GetByLanguage(string language)
        {
            SQLiteParameter[] parameters =
                {
                    new SQLiteParameter("@CoLanguage", DbType.String, 255)
                };
            parameters[0].Value = language;

            var strSqlOne = new StringBuilder();
            var rows = DbHelperSqLite.Query("select * from TbSetting where CoLanguage=@CoLanguage", parameters).Tables["ds"].Rows;
            if (rows.Count < 1)
            {
                strSqlOne.AppendFormat("insert into TbSetting(CoLanguage)");
                strSqlOne.AppendFormat(" values(@CoLanguage);");
                DbHelperSqLite.ExecuteSql(strSqlOne.ToString(), parameters);
                rows = DbHelperSqLite.Query("select * from TbSetting where CoLanguage=@CoLanguage", parameters).Tables["ds"].Rows;
            }
            return rows[0];
        }
    }
}
